/* eslint-disable max-len */
import React, { useState, useEffect } from 'react';
import {
    View, Text, ScrollView, TouchableOpacity,
} from 'react-native';
import { Calendar } from 'react-native-calendars';
import moment from 'moment';
import { WhiteSpace, Toast } from '@ant-design/react-native';
import PropTypes from 'prop-types';
import AntDesign from 'react-native-vector-icons/AntDesign';
import { debounce } from '../../utils/common';
import {
    updateUserDay, timeoutTask, exigency, selectPlan,
} from '../../comconfig/sqlfun';
import styles from './styles';

const Home = ({ navigation, route }) => {
    const [date, setDate] = useState(moment(new Date()).format('YYYY-MM-DD'));
    const [timeoutList, setTimeoutTask] = useState([]);
    const [exigencyList, setExigency] = useState([]);
    const [readyList, setReadyList] = useState([]);
    const getTimeoutTask = async () => {
        try {
            const list1 = await timeoutTask();
            const arr1 = [];
            for (let i = 0; i < list1.length; i += 1) {
                arr1.push(JSON.parse(list1[i]));
            }
            setTimeoutTask(arr1);
        } catch (error) {
            Toast.info('获取数据失败', 1, undefined, false);
        }
    };
    const getExigency = async () => {
        try {
            const list2 = await exigency();
            const arr2 = [];
            for (let i = 0; i < list2.length; i += 1) {
                arr2.push(JSON.parse(list2[i]));
            }
            setExigency(arr2);
        } catch (error) {
            Toast.info('获取数据失败', 1, undefined, false);
        }
    };
    const getReadyList = async () => {
        try {
            const list = await selectPlan(1, '');
            const arr = [];
            for (let i = 0; i < list.length; i += 1) {
                arr.push(JSON.parse(list[i]));
            }
            setReadyList(arr);
        } catch (error) {
            Toast.info('获取数据失败', 1, undefined, false);
        }
    };
    useEffect(() => {
        updateUserDay(moment(new Date()).format('YYYY-MM-DD'));
    }, []);
    useEffect(() => {
        getTimeoutTask();
        getExigency();
        getReadyList();
    }, [route]);
    return (
        <View>
            <Calendar
                // current="2022-02-15"
                minDate="1900-05-10"
                maxDate="2130-05-30"
                onDayPress={(day) => {
                    setDate(day.dateString);
                    debounce(() => navigation.push('添加日志', { date: day.dateString, home: true }));
                }}
                enableSwipeMonths
                markedDates={{
                    [date]: {
                        selected: true,
                        // disableTouchEvent: true,
                        selectedColor: '#00ADF5',
                        selectedTextColor: '#FFF',
                    },
                }}
            />
            <WhiteSpace />
            <View>
                <Text style={styles.title}>当前情况</Text>
                <View style={styles.container}>
                    <ScrollView>
                        <WhiteSpace />
                        <TouchableOpacity
                            onPress={() => navigation.jumpTo('开始')}
                        >
                            <Text style={styles.title2}>紧急事务</Text>
                        </TouchableOpacity>
                        <View>
                            {
                                exigencyList.length === 0 ? (<Text>当前无紧急事务</Text>)
                                    : exigencyList.map(ojbItem => (
                                        <View key={ojbItem.rowid} style={styles.exigencybg}>
                                            <AntDesign
                                                name="exclamationcircleo"
                                                size={18}
                                                color="#F74153"
                                                style={{ marginRight: 5 }}
                                            />
                                            <Text style={{ alignItems: 'flex-end', fontSize: 16 }}>
                                                {ojbItem.title}
                                            </Text>
                                        </View>
                                    ))
                            }
                        </View>
                        <WhiteSpace />
                        <TouchableOpacity
                            onPress={() => navigation.push('任务执行区')}
                        >
                            <Text style={styles.title2}>超时的任务</Text>
                        </TouchableOpacity>
                        <View>
                            {
                                timeoutList.length === 0 ? (<Text>当前无超时任务</Text>)
                                    : timeoutList.map(ojbItem => (
                                        <View key={ojbItem.planid} style={styles.itembg}>
                                            <Text style={styles.timeoutTitle}>{ojbItem.title}</Text>
                                            <View style={styles.row}>
                                                <View style={styles.row}>
                                                    <Text>计划于</Text>
                                                    <Text style={styles.dataFont}>
                                                        {moment(ojbItem.plantime).format('YY-MM-DD')}
                                                    </Text>
                                                    <Text>完成</Text>
                                                </View>
                                                <View style={styles.timeoutInfo}>
                                                    <Text>用时</Text>
                                                    <Text style={styles.dataFont}>
                                                        {
                                                            `${ojbItem.actuallyworktime}/${ojbItem.planworktime}`
                                                        }
                                                    </Text>
                                                </View>
                                            </View>
                                        </View>
                                    ))
                            }
                        </View>
                        <WhiteSpace />
                        <TouchableOpacity
                            onPress={() => navigation.push('任务就绪区')}
                        >
                            <Text style={styles.title2}>就绪的任务</Text>
                        </TouchableOpacity>
                        <View>
                            {
                                readyList.length === 0 ? (<Text>当前无就绪任务</Text>)
                                    : readyList.map(ojbItem => (
                                        <View key={ojbItem.planid} style={styles.itembg}>
                                            <Text style={styles.redayTitle}>{ojbItem.title}</Text>
                                        </View>
                                    ))
                            }
                        </View>
                        <WhiteSpace />
                    </ScrollView>
                </View>
            </View>
        </View>
    );
};

Home.defaultProps = {
    route: {},
};
Home.propTypes = {
    route: PropTypes.objectOf(PropTypes.any),
};
export default Home;
